package com.restaurantpicker.util;

import java.io.StringWriter;
import java.util.ArrayList;

import org.codehaus.jackson.map.ObjectMapper;

import com.restaurantpicker.model.Place;
import com.restaurantpicker.model.PlacesList;

public class PickerUtility {
	
	public static final String EMPTY_PLACES_LIST = "{\"places\":[]}";
	public static final String EMPTY_PLACE = "{\"place\":[]}";
	
	public static String getAllRestJSON(ArrayList<Place> places){
		StringWriter writer = new StringWriter();
		
		PlacesList placesList = new PlacesList();
		placesList.setPlaces(places);
		
		try{
			ObjectMapper mapper = new ObjectMapper();
			mapper.writeValue(writer, placesList);
		}catch(Exception e){
			return EMPTY_PLACES_LIST;
		}		
		
		return writer.toString();
	}
	
	public static String getRestJSON(Place place){
		StringWriter writer = new StringWriter();
		
		try{
			ObjectMapper mapper = new ObjectMapper();
			mapper.writeValue(writer, place);
		}catch(Exception e){
			return EMPTY_PLACE;
		}		
		
		return writer.toString();
	}	

}
